#include "chatting_room.h"
#include "ui_chatting_room.h"
#include <QDebug>
#include "mypushbutton.h"
#include <QTimer>
#include <QMutex>

extern std::map<std::string, std::vector<std::string> > global_ip2msg;
extern QMutex global_ip2msg_mutex;

Chatting_room::Chatting_room(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Chatting_room),
    chat_client(ChatClient::getChatClientInstance())
{
    ui->setupUi(this);
    timer = new QTimer(this);   //新建定时器
    srtimer = new QTimer(this);
    //connect(srtimer,SIGNAL(timeout()),this,SLOT(showSyc()));
    connect(timer,SIGNAL(timeout()),this,SLOT(showHis()));//关联定时器计满信号和相应的槽函数
    srtimer->start(1000);
}


void Chatting_room::showSyc()
{

    if(!info.isEmpty())
        srtimer->stop();
    QString s = ui->history->toPlainText();
    if(s.isEmpty()){
            ui->history->setText(info);
    }
    else
        ui->history->append(info);

}


Chatting_room::~Chatting_room()
{
    delete ui;
}

void Chatting_room::getInfo(QString s,Chatting_room * adds){

    global_ip2msg_mutex.lock();
    for (auto iter = global_ip2msg.begin(); iter != global_ip2msg.end(); ++iter) {
        QString dest_ip = QString::fromStdString(iter->first);

        if(!userList.contains(dest_ip)){
        MyPushButton *mypb = new MyPushButton();
        mypb->QB->setText(dest_ip);
        ui->vh->setDirection(QBoxLayout::TopToBottom);
        ui->vh->addWidget(mypb->QB);

        userList.append(dest_ip);
        QObject::connect(mypb->QB,SIGNAL(clicked()),mypb,SLOT(on_clicked()));
        }
    }
    global_ip2msg_mutex.unlock();
    //定义按钮位置并初始化
    int Dx,Dy;
    int n = userList.size();


    Dx = 0;
    Dy = 0;

    //qDebug() << userList.size();
    if(userList.size() == 0)
    {
        userList.append(s);
        MyPushButton *mypb = new MyPushButton();
        mypb->QB->setText(s);
        ui->vh->setDirection(QBoxLayout::TopToBottom);
        ui->vh->addWidget(mypb->QB);

        QObject::connect(mypb->QB,SIGNAL(clicked()),mypb,SLOT(on_clicked()));

    }
    else{
        if(userList.contains(s))
            qDebug()<<"ok";
        else
        {
            userList.append(s);
            MyPushButton *mypb = new MyPushButton();
            mypb->QB->setText(s);
            ui->vh->setDirection(QBoxLayout::TopToBottom);
            ui->vh->addWidget(mypb->QB);

            QObject::connect(mypb->QB,SIGNAL(clicked()),mypb,SLOT(on_clicked()));
            Dx = 0;
            Dy = n*30;
        }
    }

     this->show();
    timer->start(100);
}

void Chatting_room::showHis(){
    IP = info.toStdString();
    global_ip2msg_mutex.lock();
    std::vector<std::string> msg_vec = global_ip2msg[IP];
    global_ip2msg.erase(IP);
    global_ip2msg_mutex.unlock();
    QString show_msg;

    for (auto iter = msg_vec.begin(); iter != msg_vec.end(); ++iter) {
        show_msg += info;
        show_msg += ":";
        show_msg += QString::fromStdString(*iter);
        show_msg += "\n";
    }
    if(!show_msg.isEmpty())
        ui->history->append(show_msg);
}


void Chatting_room::on_Quit_clicked()
{
    this->close();
}

void Chatting_room::on_send_clicked()
{
    QString str = ui->lineEdit->text();
    std::string tmp_info = info.toStdString();
    IP = tmp_info.substr(0, tmp_info.find("#"));
    chat_client.sendMsgSlots(IP, str.toStdString());
    qDebug() << QString::fromStdString(IP);

    ui->history->append(str);
    ui->lineEdit->clear();
}
